/*============================================================================
 PROJECT: TalkWrench
 FILE:    UIBarButtonItem+Customizes.m
 AUTHOR:  Nguyen Minh Khoai
 DATE:    1/31/13
 =============================================================================*/

/*============================================================================
 IMPORT
 =============================================================================*/
#import "UIBarButtonItem+Customizes.h"
#import "IBHelper.h"

/*============================================================================
 PRIVATE MACRO
 =============================================================================*/
#define kButtonPadding  5

/*============================================================================
 PRIVATE INTERFACE
 =============================================================================*/

@implementation UIBarButtonItem (Customizes)

+ (UIBarButtonItem *)barButtonItemWithTarget:(id)target
                                    selector:(SEL)sel
                                       title:(NSString *)title
                                    iconName:(NSString *)iconName
                              backgroundName:(NSString *)backgroundName
                                     padding:(CGFloat)padding {
    
    /* create icon and background images */
    UIImage *image = [[IBHelper sharedUIHelper] loadImage:iconName];
    UIImage *backgroundImage = [[IBHelper sharedUIHelper] middleStretchImage:backgroundName];
    
    /* create container view */
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor clearColor];
   
    /* create custom button */
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = FontOpenSansSemiBoldWithSize(14);
    [containerView addSubview:button];
    
    /* update frame basing on title */
    CGRect rect;
    if(title.length > 0) {
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
        CGSize size = [Utils constrainSizeForLabel:button.titleLabel autoFitSize:AutoFitSizeHeight];
        rect = CGRectMake(0, 0, size.width + (IS_IPAD ? 20 : 10), 30);
    } else {
        [button setImage:image forState:UIControlStateNormal];
        rect = CGRectMake(0, 0, image.size.width, image.size.height);
    }
    button.frame = rect;
    
    /* register button event */
    if(target) {
        [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    }
    
    /* update frame for container view */
    rect.size.width += fabs(padding);
    containerView.frame = rect;
    
    /* update frame for button */
    if(padding < 0) {
        rect = button.frame;
        rect.origin.x -= padding;
        button.frame = rect;
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:containerView];
}

+ (UIBarButtonItem *)barButtonItemWithTarget:(id)target
                                    selector:(SEL)sel
                                       title:(NSString *)title
                                   imageName:(NSString *)name {
    BOOL isColor = NO;
    UIImage *image = [[IBHelper sharedUIHelper] loadCommonImage:name];
    
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = FontBoldWithSize(14);
    
    if(isColor) {
        CGSize size = [Utils constrainSizeForLabel:button.titleLabel autoFitSize:AutoFitSizeHeight];
        CGRect rect = CGRectMake(0, 0, size.width + 2 * kButtonPadding, size.height + 2 * kButtonPadding);
        button.frame = rect;
        
        LayerConer(button.layer);
        LayerBorderWithSizeColor(button.layer, 0.2, [UIColor lightGrayColor]);
    } else {
        if(title) {
            CGSize size = [Utils constrainSizeForLabel:button.titleLabel autoFitSize:AutoFitSizeHeight];
            CGRect rect = CGRectMake(0, 0, size.width, size.height);
            button.frame = rect;
        } else {
            CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
            button.frame = rect;
        }
    }
    
    [button setBackgroundImage:image forState:UIControlStateNormal];
    
    if(target) {
        [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    }
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}
@end
